package com.cby.library.data.repository.parser

import com.cby.library.data.model.GameInfoBean
import com.cby.library.data.model.ModuleBean
import com.cby.library.data.model.RecAdBean
import com.cby.library.data.model.*
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type
import java.util.*

/**
 * 首页复杂类型json
 */
class HomeModuleJsonParser : ModuleJsonParser() {

    private val recAdType: Type = object : TypeToken<MutableList<RecAdBean?>?>() {}.type
    private val gameType: Type = object : TypeToken<MutableList<GameInfoBean?>?>() {}.type
    private val displayTypes = Arrays.asList<String>(
        TYPE_AD_1,
        TYPE_AD_2,
        TYPE_GRID_1,
        TYPE_GRID_2,
        TYPE_GRID_4
    )

    override fun convertModuleJson(
        moduleJson: JsonObject?,
        moduleBean: ModuleBean?
    ) {
        moduleBean?.let {
            val listJson: JsonElement? = moduleJson?.get("list")
            if (MODULE_TYPE_ADS == moduleBean?.moduleType) {
                it.list = gson.fromJson<Any>(listJson, recAdType) as MutableList<Any>?
            } else {
                it.list = gson.fromJson<Any>(listJson, gameType) as MutableList<Any>?
            }


            if (!displayTypes.contains(it.displayStyle)
                || it.list == null || it.list!!.isEmpty()
            ) {
                //非当前版本的设置为none
                it.displayStyle = TYPE_NONE
            }
        }


    }

    companion object {
        private const val MODULE_TYPE_ADS = "ads"
    }
}